//
//  HENoticePickerCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/12/31.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HENoticePickerCell.h"
#import "HEPickerResModel.h"
#import "HEHealthAlarmModel.h"

@interface HENoticePickerCell ()<UIPickerViewDataSource, UIPickerViewDelegate>

/** 日期选择器 */
@property (nonatomic, strong) UIPickerView *pickerView;

/// 数据源
@property (nonatomic, strong) NSArray <NSArray *> *dataSource;

@property (nonatomic, strong) NSMutableArray <HEHealthAlarmModel *>*selectArr;

@property (nonatomic, strong) HEHealthAlarmModel *model;

@end

@implementation HENoticePickerCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;

        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    [self initData];
    [self initSelectModel];

    [self.contentView addSubview:self.pickerView];
    
    [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(5);
        make.right.equalTo(self.contentView);
        make.left.equalTo(self.contentView);
        make.height.offset(200);
        make.bottom.equalTo(self.contentView).offset(-5);
    }];
}

- (void)initData {
    NSMutableArray *hours = @[].mutableCopy;
    for (int i = 0; i < 24; i++) {
        [hours addObject:[NSString stringWithFormat:@"%d", i]];
    }
    
    NSMutableArray *mines = @[].mutableCopy;
    for (int i = 0; i < 60; i++) {
        [mines addObject:[NSString stringWithFormat:@"%d", i]];
    }
    self.dataSource = @[hours, mines];
}

- (void)initSelectModel {
    [self.selectArr removeAllObjects];
    
    for (int i = 0; i < _dataSource.count; i++) {
        NSArray *culms = _dataSource[i];
        HEPickerResModel *model = [HEPickerResModel new];
        model.compent = i;
        model.row = 0;
        model.value = culms[0];
        [self.selectArr addObject:model];
    }
}

- (void)configCellWithModel:(HEHealthAlarmModel *)model indexPath:(NSIndexPath *)indexPath {
     
    _model = model;
    NSArray *arr = [model.alarm_time componentsSeparatedByString:@":"];
    for (int i = 0; i< arr.count; i++) { // 列
        NSArray *rows = _dataSource[i];
        NSString *value = arr[i];
        HEPickerResModel *model = self.selectArr[i];

        for (int j = 0; j < rows.count; j++) { // 行
            NSString *rowValue = rows[j];
            if ([value isEqualToString: rowValue]) {
                model.row = j;
                model.value = rowValue;
                [self.pickerView selectRow:j inComponent:i animated:NO];
                break;
            }
        }
    }
}


#pragma mark - UIPickerViewDataSource
// 1. 设置 picker 的列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

// 2. 设置 picker 每列的行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    NSArray *rowsArr = _dataSource[component];
    return rowsArr.count;
}

#pragma mark - UIPickerViewDelegate
// 3. 设置 picker 的 显示内容
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view {
    
    // 设置分割线的颜色
    for (UIView *subView in pickerView.subviews) {
        if (subView && [subView isKindOfClass:[UIView class]] && subView.frame.size.height <= 1) {
            subView.backgroundColor = [UIColor colorWithRed:195/255.0 green:195/255.0 blue:195/255.0 alpha:1.0];
        }
    }
    
    UILabel *label = (UILabel *)view;
    if (!label) {
        label = [[UILabel alloc]init];
        label.backgroundColor = [UIColor clearColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:18.0f];
        label.textColor = [UIColor blackColor];
        // 字体自适应属性
        label.adjustsFontSizeToFitWidth = YES;
        // 自适应最小字体缩放比例
        label.minimumScaleFactor = 0.5f;
    }
    
    // 给选择器上的label赋值
    NSArray *rowsArr = _dataSource[component];
    label.text = rowsArr[row];
    
    return label;
}

// 4. 日期选择器 每次滚动后的回调方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    NSMutableArray *times = @[].mutableCopy;
    for (HEPickerResModel *model in self.selectArr) {
        if (model.compent == component) {
            NSArray *rowsArr = _dataSource[component];
            model.row = row;
            model.value = rowsArr[row];
        }
        [times addObject:model.value];
    }
    
    _model.alarm_time = [times componentsJoinedByString:@":"];
}

// 设置行高
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 35.0f;
}

#pragma mark - 选择器
- (UIPickerView *)pickerView {
    if (!_pickerView) {
        _pickerView = [[UIPickerView alloc] init];
        _pickerView.backgroundColor = [UIColor whiteColor];
        _pickerView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
        _pickerView.dataSource = self;
        _pickerView.delegate = self;
    }
    return _pickerView;
}

- (NSMutableArray <HEHealthAlarmModel *>*)selectArr {
    if (!_selectArr) {
        _selectArr = [NSMutableArray array];
    }
    return _selectArr;
}

@end
